package com.hp.attendance.serlvet.attendanceManager;

import com.hp.attendance.entity.Attendance;
import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.dto.AttEmpDto;
import com.hp.attendance.service.AttendanceService;
import com.hp.attendance.service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/attMangeList")
public class attendanceListPreServlet extends HttpServlet {

    private AttendanceService attendanceService=new AttendanceService();
    private EmployeeService employeeService=new EmployeeService();



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Employee> listEmp = employeeService.getList();
            List<Attendance> list = attendanceService.queryAttList();
            List<AttEmpDto> empDtos = new ArrayList<>();
            for (Attendance attendance : list) {
                AttEmpDto aed = new AttEmpDto();
                aed.setAttendance(attendance);
                for (Employee employee : listEmp) {
                    if (attendance.getEmployee_number().equals(employee.getEmployee_number())){
                        aed.setName(employee.getName());
                    }

                }
                empDtos.add(aed);

            }

            req.setAttribute("list",empDtos);
            req.getRequestDispatcher("/pages/attendance_list.jsp").forward(req,resp);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
